<template>
  <div class="icon-index">
    <el-input
      v-model="icon"
      :prefix-icon="icon"
      clearable
      readonly
      :placeholder="placeholder"
      :disabled="disabled"
      :size="size"
      @focus="onFocus"
      @blur="onBlur"
    />
    <el-popover
      v-model="visible"
      placement="bottom"
      width="500"
      trigger="manual"
    >
      <div class="icon-context" style="overflow: hidden;">
        <el-scrollbar style="height: 100%;overflow: hidden;">
          <div v-for="(item,index) in list" :key="index" class="icon-item">
            <el-button
              size="mini"
              :icon="item"
              :type="item === icon?'primary':''"
              @click="onSelect(item)"
            />
          </div>
        </el-scrollbar>
      </div>
    </el-popover>
  </div>
</template>

<script>
import icons from './icons'

export default {
  name: 'IconPicker',
  props: {
    placeholder: {
      type: String,
      default: '请选择一个图标'
    },
    disabled: {
      type: Boolean,
      default: false
    },
    size: {
      type: String,
      default: ''
    },
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      icon: '',
      visible: false,
      list: []
    }
  },
  mounted() {
    this.list = icons
  },
  watch: {
    value: {
      handler(val) {
        this.icon = val
      }
    }
  },
  methods: {
    onFocus() {
      this.visible = true
    },
    onBlur() {
      this.visible = false
    },
    onSelect(icon) {
      this.icon = icon
      this.visible = false
      this.$emit('input', this.icon)
    }
  }
}
</script>

<style scoped>
.icon-index {
  width: 200px;
}

.icon-context {
  height: 200px;
  text-align: center;
}

.icon-item {
  margin: 5px 5px;
  display: inline-block;
}

/deep/ .el-scrollbar__wrap {
  overflow-x: hidden !important;
  overflow-y: scroll;
}

/deep/ .el-scrollbar__bar.is-horizontal {
  display: none;
}

</style>
